package session

import (
	"github.com/astaxie/beego/logs"
	"github.com/astaxie/beego/orm"
	"github.com/robfig/cron"
	"myadmin/sys/cache/mapstore"
	"time"
)

/*
定时清除过期session
 */


func ClearnSession(){

	sql:="delete from sys_session where create_time > ? "
	o:=orm.NewOrm()
	_,err:=o.Raw(sql,time.Now().Unix()-mapstore.Get("exp").(int64)).Exec()
	if err!=nil{
		logs.Error("清理session失败:%s",err)
	}else{
		logs.Error("清理session成功")
	}
}

func SetTimeClearn(){
	//每隔1分钟执行一次：0 */1 * * * ?
	//每天23点执行一次：0 0 23 * * ?
	c := cron.New()
	c.AddFunc("0 0 3 * * ?",ClearnSession)
	c.Start()
}
